function validateFormSalle()
{
	var valide = true;
	if($('salle_nom_salle').value == ''){
		valide = false
		$('error_nom_salle').innerHTML = "Le nom de la salle est obligatoire";
	}
	return valide;
}

function validateFormMachine(id_salle)
{
	var exp=new RegExp("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");
	var valide = true;
	if($('new_machine_'+id_salle).elements['machine[nom_machine]'].getValue() == ''){
		valide = false;
		$('error_nom_machine_'+id_salle).innerHTML = "Le nom est obligatoire";
	}else if($('new_machine_'+id_salle).elements['machine[nom_machine]'].getValue().length > 15){
		valide = false;
		$('error_nom_machine_'+id_salle).innerHTML = "Le nom ne doit pas dépasser 15 caractères";
	}else{
		$('error_nom_machine_'+id_salle).innerHTML = "";
	}
	
	if($('new_machine_'+id_salle).elements['machine[adresse_ip]'].getValue() == ''){
		valide = false;
		$('error_adresse_ip_'+id_salle).innerHTML = "Adresse IP est obligatoire";
	}else if (!exp.test($('new_machine_'+id_salle).elements['machine[adresse_ip]'].getValue())){
		valide = false;
		$('error_adresse_ip_'+id_salle).innerHTML = "Adresse IP est au mauvais format";
	}else{
		$('error_adresse_ip_'+id_salle).innerHTML = "";
	}
	return valide;
}

function validateFormImport()
{
	var valide = true;
	if($('choix_import_reseau_entier').getValue() == null && $('choix_import_reseau_plage').getValue() == null ){
		valide = false;
		$('error_form_import').innerHTML = "Vous devez choisir un type d'import";
	}
	if($('choix_import_reseau_plage').checked && ($('debut_ip_1').getValue() == '' || $('debut_ip_2').getValue() == '')){
		valide = false;
		$('error_form_import').innerHTML = "Vous devez préciser une plage";
	}
	ipdebut = "172.16."+$('debut_ip_1').getValue()+'.'+$('debut_ip_2').getValue();
	ipfin = "172.16."+$('fin_ip_1').getValue()+'.'+$('fin_ip_2').getValue();
	if($('choix_import_reseau_plage').checked && ((ip2long(ipdebut)) > (ip2long(ipfin)) )){
				valide = false;
		$('error_form_import').innerHTML = "La première adresse ip est supérieure à la deuxième ";
	}
	//if($('choix_import_reseau_plage').checked && (($('debut_ip_1').getValue() > 254) || ($('fin_ip_1').getValue() > 254 ) || $('fin_ip_2').getValue() > 254)){
	//	valide = false;
	//	$('error_form_import').innerHTML = "Les valeurs ne peuvent pas dépasser 254";
	//	document.getElementById('debut_ip_1').value='';
		
	//	document.getElementById('fin_ip_1').value='';
	//	document.getElementById('fin_ip_2').value='';
	//}
	if($('choix_import_reseau_plage').checked && (($('debut_ip_1').getValue() > 255))) {
		valide = false;
		$('error_form_import').innerHTML = "Les valeurs ne peuvent pas dépasser 255";
		document.getElementById('debut_ip_1').value='';
	}
	if($('choix_import_reseau_plage').checked && (($('debut_ip_2').getValue() > 254))) {
		valide = false;
		$('error_form_import').innerHTML = "Les valeurs ne peuvent pas dépasser 254";
		document.getElementById('debut_ip_2').value='';
	}
	if($('choix_import_reseau_plage').checked && (($('fin_ip_1').getValue() > 255))) {
		valide = false;
		$('error_form_import').innerHTML = "Les valeurs ne peuvent pas dépasser 255";
		document.getElementById('fin_ip_1').value='';
	}
	if($('choix_import_reseau_plage').checked && (($('fin_ip_2').getValue() > 254))) {
		valide = false;
		$('error_form_import').innerHTML = "Les valeurs ne peuvent pas dépasser 254 ";
		document.getElementById('fin_ip_2').value='';
	}
	return valide;
}


function ip2long(IP) {
    // Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/ip2long    // +   original by: Waldo Malqui Silva
    // +   improved by: Victor
    // +    revised by: fearphage (http://http/my.opera.com/fearphage/)
    // +    revised by: Theriault
    // *     example 1: ip2long('192.0.34.166');    // *     returns 1: 3221234342
    // *     example 2: ip2long('0.0xABCDEF');
    // *     returns 2: 11259375
    // *     example 3: ip2long('255.255.255.256');
    // *     returns 3: false    var i = 0;
    // PHP allows decimal, octal, and hexadecimal IP components.
    // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
    IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); // Verify IP format.
    if (!IP) {        return false;} // Invalid format.
    
    //Reuse IP variable for component counter.
    IP[0] = 0;
    for (i = 1; i < 5; i += 1) {        IP[0] += !! ((IP[i] || '').length);
        IP[i] = parseInt(IP[i]) || 0;
    }
    // Continue to use IP for overflow values.
    // PHP does not allow any component to overflow.    IP.push(256, 256, 256, 256);
    // Recalculate overflow of last component supplied to make up for missing components.
    IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
    if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
        return false;    }
    return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
}